package com.stx.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.stx.dao.FactoryDao;
import com.stx.dao.MedicineDao;
import com.stx.entity.TMedicine;

/**
 * Servlet implementation class maddservlet
 */
public class maddservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public maddservlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String mname = req.getParameter("mname");
		String mtname = req.getParameter("mtypename");
		String mfoname = req.getParameter("mformname");
		String mfaname = req.getParameter("mfactoryname");
		String newmfname = req.getParameter("newmfactoryname");
		String newmfaddress = req.getParameter("newmfactoryaddress");
		String newmfphone = req.getParameter("newmfactoryphone");
		String prescription = req.getParameter("prescription");
		String expiration = req.getParameter("expiration");
		String mcount = req.getParameter("mcount");
		String mfunction = req.getParameter("mfunction");
		System.out.println(prescription);
		String did = (String)req.getSession().getAttribute("did");
		FactoryDao fd = new FactoryDao();
		MedicineDao md = new MedicineDao();
		if(newmfname==null){
			fd.addFactory(newmfname,newmfaddress,newmfphone);
			md.addmedicine(mname, mtname, mfoname,fd.findmFactoryid(newmfname), prescription, expiration, mcount, mfunction);
		}else{
			md.addmedicine(mname, mtname, mfoname,mfaname, prescription, expiration, mcount, mfunction);
		}
		List<TMedicine> medlist = md.showAllmedicine();
		req.setAttribute("medlist", medlist);
		req.getRequestDispatcher("/admin/showdm.jsp").forward(req, resp);
		
	}

}
